package com.mangofactory.aop.configuration
{
	import flash.utils.getDefinitionByName;
	
	import mx.core.FlexGlobals;
	
	import org.as3commons.bytecode.reflect.ByteCodeType;
	import org.as3commons.reflect.Type;
	import org.as3commons.reflect.TypeCache;

	public class ReflectionClassRegistry implements ClassRegistry
	{
		private var typeCache:TypeCache;
		public function ReflectionClassRegistry(typeCache:TypeCache=null)
		{
			if (!typeCache)
			{
				typeCache = ByteCodeType.getTypeProvider().getTypeCache();
				if (typeCache.getKeys().length == 0)
				{
					ByteCodeType.fromLoader(FlexGlobals.topLevelApplication.loaderInfo);
				}
			}
				
			this.typeCache = typeCache;
		}
		
		public function getClasses():Vector.<Class>
		{
			var classes:Vector.<Class> = new Vector.<Class>();
			for each (var className:String in typeCache.getKeys())
			{
				var clazz:Class;
				try {
					clazz = getDefinitionByName(className) as Class;
				} catch (e:Error) {
					// TODO : Need to figure out how to only iterate public classes
					// Will be thrown on internal classes.
					continue;
				}
				classes.push(clazz);
			}
			return classes;
		}
		
		public function addClass(clazz:Class):void
		{
		}
		
		public function containsMatchingClass(clazz:Class):Boolean
		{
			return false;
		}
	}
}